/**
 * Created by judian @2018-10-18
 */

import * as api from './index'
import config from 'utils/config'
const urlnc = config.API_NC + '/xfapi/index.php'

class PublicService {
    // 获取key 
    getAuthKey({
        unionid
    }) {
        let url = `${urlnc}?act=setting_new&op=getMemberForUnionid`
        return api.fetch(url, { unionid }).then(ret => {
            if (ret.code == 200) {
                if (ret.datas && ret.datas.key) {
                    return ret.datas
                } else {
                    return null
                }

            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 本地，二手获取信息价格
    initInfoPrice({
        key,
        category_id
    }) {
        let url = `${urlnc}?act=agent&op=get_price`
        return api.fetch(url, { key, category_id }).then(ret => {
            if (ret.code == 200) {
                if (ret.datas && ret.datas.price) {
                    return ret.datas
                } else {
                    xmview.showAlert('提示', ret.datas.error)
                    return null
                }
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 获取定位信息
    getLocationArea({
        unionid
    }) {
        let url = `${urlnc}?act=setting_new&op=getAreaForUnionid`
        return api.fetch(url, { unionid }).then(ret => {
            if (ret.code == 200) {
                if (ret.datas && ret.datas.provinceid) {
                    sessionStorage.setItem('areap_id', ret.datas.provinceid)
                    sessionStorage.setItem('areac_id', ret.datas.cityid)
                    sessionStorage.setItem('areax_id', ret.datas.areaid)
                    return ret.datas
                } else {
                    return null
                }

            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 广告信息类型
    getAdverType({
        fromTypeTxt,
        type
    }) {
        let url = `${urlnc}?act=${fromTypeTxt}&op=get_adver_class`
        return api.fetch(url, { type }).then(ret => {
            if (ret.code == 200) {
                return ret.datas.map(item => {
                    let obj = {}
                    obj.value = item.id
                    obj.text = item.adc_type
                    return obj
                })

            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 二手信息类型
    getSecondType({
        fromTypeTxt
    }) {
        let url = `${urlnc}?act=${fromTypeTxt}&op=get_cate`
        return api.fetch(url).then(ret => {
            if (ret.code == 200) {
                return ret.datas.map(item => {
                    let obj = {}
                    obj.value = item.id
                    obj.text = item.name
                    return obj
                })

            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 本地信息类型
    getLocalType({
        fromTypeTxt,
        category_id
    }) {
        let url = `${urlnc}?act=${fromTypeTxt}&op=get_general_class`
        return api.fetch(url, { category_id }).then(ret => {
            if (ret.code == 200) {
                let data = ret.datas[0].child_list
                return data.map(item => {
                    let obj = {}
                    obj.value = item.id
                    obj.text = item.class_name
                    return obj
                })

            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 获取时长信息
    getAdverPrice({
        fromTypeTxt,
        type
    }) {
        let url = `${urlnc}?act=${fromTypeTxt}&op=get_adver_price`
        return api.fetch(url, { type }).then(ret => {
            if (ret.code == 200) {
                return ret.datas.map(item => {
                    let obj = {}
                    obj.value = item.id
                    obj.text = item.adver_longtime
                    obj.price = item.adver_price
                    return obj
                })
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 获取分享费用
    getSharePrice({
        share_yibi_id
    }) {
        let url = `${urlnc}?act=setting_new&op=getSharePrice`
        return api.fetch(url, { share_yibi_id }).then(ret => {
            if (ret.code == 200) {
                return ret.datas
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 获取置顶费用
    getTopPrice() {
        let url = `${urlnc}?act=test&op=get_top_pricing`
        return api.fetch(url).then(ret => {
            if (ret.code == 200) {
                return ret.datas
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 发布金牌广告 
    publicAdver(obj) {
        let url = `${urlnc}?act=${obj.fromTypeTxt}&op=adverAdd`
        return api.post(url, obj).then(ret => {
            ret = ret.data
            if (ret.code == 200) {
                if (ret.datas.nextpage) {
                    sessionStorage.setItem('nextpage', ret.datas.nextpage)
                    return ret.datas.info
                } else {
                    xmview.showAlert('提示', ret.datas.error || '没有nextpage')
                    return null
                }

            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 发布二手信息
    publicSecond(obj) {
        let url = `${urlnc}?act=${obj.fromTypeTxt}&op=add`
        return api.post(url, obj).then(ret => {
            ret = ret.data
            if (ret.code == 200) {
                if (ret.datas.nextpage) {
                    sessionStorage.setItem('nextpage', ret.datas.nextpage)
                    return ret.datas.info
                } else {
                    xmview.showAlert('提示', ret.datas.error || '没有nextpage')
                    return null
                }
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 发布本地信息
    publicLocal(obj) {
        let url = `${urlnc}?act=${obj.fromTypeTxt}&op=generalAdd`
        return api.post(url, obj).then(ret => {
            ret = ret.data
            if (ret.code == 200) {
                if (ret.datas.nextpage) {
                    sessionStorage.setItem('nextpage', ret.datas.nextpage)
                    return ret.datas.info
                } else {
                    xmview.showAlert('提示', ret.datas.error || '没有nextpage')
                    return null
                }
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 微信支付
    toWxPay({
        info,
        pdc_sn,
        actualPrice,
        key,
        wx
    }) {
        let url = `${urlnc}?act=voucher_payment&op=weixin_pay_test`
        return api.post(url, { info, pdc_sn, actualPrice, key, wx }).then(ret => {
            ret = ret.data
            if (ret.code == 200) {
                return ret.datas
            } else {
                return Promise.reject(ret)
            }
        })
    }

    // 初始化微信config
    initWxConfig({
        url
    }) {
        let urL = `${urlnc}?act=setting_new&op=get_sign_package`
        return api.fetch(urL, { url }).then(ret => {
            if (ret.code == 200) {
                return ret.datas
            } else {
                return Promise.reject(ret)
            }
        })
    }
}
export default new PublicService()